package com.octopus.kernal.service;

import java.io.Serializable;
import java.util.List;

import com.octopus.kernal.controller.AbstractSearchCommand;
import com.octopus.kernal.dao.GenericHibernateDao;
import com.octopus.kernal.model.BaseStruct;

public abstract class AbstractEntityService<T extends BaseStruct, PK extends Serializable, DAO extends GenericHibernateDao<T, PK>> {
	
	protected DAO getDao(){
		return null;
	}
	
	public void createEntity(T entity){
		getDao().save(entity);
	}
	
	public T getEntity(PK id){
		return getDao().get(id);
	}
	
	public void updateEntity(T entity){
		getDao().saveOrUpdate(entity);
	}
	
	public void deleteEntity(T entity){
		getDao().delete(entity);
	}
	
	public void deleteEntity(PK id){
		getDao().deleteByKey(id);
	}
	
	public List<T> getEntitiesAll(){
		return getDao().loadAll();
	}
	
	public List<T> getEntitiesPaged(int offset, int length){
		return getDao().loadAll(offset, length);
	}
	
	public List<T> getEntitiesSortedAll(String[] propertyNames){
		return getDao().findEqualByEntity(null, propertyNames);
	}
	
	public List<T> getEntitiesSortedPaged(String[] propertyNames, int offset, int length){
		return getDao().findEqualByEntity(null, propertyNames, offset, length);
	}
	
	public int searchCount(AbstractSearchCommand<T> command){
		return getDao().searchCount(command);
	}
	
	public List<T> search(AbstractSearchCommand<T> command){
		command.setTotalNumber(getDao().searchCount(command));
		return getDao().search(command);
	}
	
	public List<T> findEntities(AbstractSearchCommand<T> command){
		if(command.getLike())
			command.setTotalNumber(getDao().countLikeByEntity(command.getEntity()));
		else
			command.setTotalNumber(getDao().countEqualByEntity(command.getEntity()));
		if(command.getPropertiesSorted() == null)
			return this.findEntitiesPaged(command.getEntity(), command.getOffset(), command.getMaxPageItems(), command.getLike());
		else
			return this.findEntitiesSortedPaged(command.getEntity(), command.getPropertiesSorted(), command.getOffset(), command.getMaxPageItems(), command.getLike());
	}
	
	public List<T> findEntitiesAll(T entity, boolean like){
		if(like)
			return getDao().findLikeByEntity(entity, null);
		else
			return getDao().findEqualByEntity(entity, null);
	}
	
	public List<T> findEntitiesPaged(T entity, int offset, int length, boolean like){
		if(like)
			return getDao().findLikeByEntity(entity, null, offset, length);
		else
			return getDao().findEqualByEntity(entity, null, offset, length);
	}
	
	public List<T> findEntitiesSortedAll(T entity, String[] propertyNames, boolean like){
		if(like)
			return getDao().findLikeByEntity(entity, propertyNames);
		else
			return getDao().findEqualByEntity(entity, propertyNames);
	}
	
	public List<T> findEntitiesSortedPaged(T entity, String[] propertyNames, int offset, int length, boolean like){
		if(like)
			return getDao().findLikeByEntity(entity, propertyNames, offset, length);
		else
			return getDao().findEqualByEntity(entity, propertyNames, offset, length);
	}

}
